import request from "@/utils/request";
import {QueueRoomModel} from "@/model/QueueRoomModel";
import {DeviceModel} from "@/model/DeviceModel";

/**
 * 诊室相关API
 */
export default class RoomApi {

    /**
     * 查询类目列表
     *
     * @param pageIndex
     * @param pageSize
     */
    public static listRegRoom(): Promise<QueueRoomModel[]> {
        return new Promise((resolve, reject) => {
            request({
                url: '/queue/room/list',
                method: 'get'
            }).then((res: any) => {
                if (res.code === 200) {
                    if (res?.data) {
                        res.data = res.data.map(item => new QueueRoomModel().deserialize(item))
                    }
                    resolve(res.data)
                } else {
                    reject(res)
                }
            }, ()=> {reject()})
        })
    }

    public static saveRegRoom(itemModel: QueueRoomModel): Promise<QueueRoomModel> {
        return new Promise((resolve, reject) => {
            request({
                url: '/queue/room/save',
                method: 'POST',
                data: itemModel
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(new QueueRoomModel().deserialize(res.data))
                } else {
                    reject(res)
                }
            }, ()=> {reject()})
        })
    }

    public static updateRegRoom(id: string, room: QueueRoomModel): Promise<boolean> {
        return new Promise((resolve, reject) => {
            request({
                url: `/queue/room/${id}`,
                method: 'PUT',
                data: room
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(true)
                } else {
                    reject()
                }
            }, ()=> {reject()})
        })
    }

    public static deleteRegRoom(id: string): Promise<boolean> {
        return new Promise((resolve, reject) => {
            request({
                url: `/queue/room/${id}`,
                method: 'DELETE'
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(true)
                } else {
                    reject()
                }
            }, ()=> {reject()})
        })
    }

    /**
     * 查询诊室下医生终端列表
     */
    public static listRoomTerminal(roomId: string, usedType: number): Promise<DeviceModel[]> {
        return new Promise((resolve, reject) => {
            request({
                url: `/queue/room/${roomId}/terminal`,
                method: 'get',
                params: {
                    usedType: usedType || undefined
                }
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(res.data.map(item => new DeviceModel().deserialize(item)))
                } else {
                    reject(res)
                }
            }, ()=> {reject()})
        })
    }

    /**
     * 为诊室分配医生客户端设备
     */
    public static addRoomTerminal(roomId: string, deviceIds: string[], usedType: number): Promise<boolean> {
        return new Promise((resolve, reject) => {
            request({
                url: `/queue/room/${roomId}/terminal?usedType=${usedType}`,
                method: 'post',
                data: deviceIds
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(res)
                } else {
                    reject(res)
                }
            }, ()=> {reject()})
        })
    }

    /**
     * 为诊室移除医生客户端设备
     */
    public static removeRoomTerminal(roomId: string, deviceId: string): Promise<boolean> {
        return new Promise((resolve, reject) => {
            request({
                url: `/queue/room/${roomId}/terminal/${deviceId}`,
                method: 'delete'
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(res)
                } else {
                    reject(res)
                }
            }, ()=> {reject()})
        })
    }
}

